package com.dcxs.shop.module.category.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dcxs.shop.common.utils.PageUtils;
import com.dcxs.shop.common.utils.Query;
import com.dcxs.shop.common.utils.RedisUtils;
import com.dcxs.shop.module.admin.entity.JsBannerPic;
import com.dcxs.shop.module.admin.service.IJsBannerPicService;
import com.dcxs.shop.module.category.entity.JsCategoryOne;
import com.dcxs.shop.module.category.entity.JsCategoryThree;
import com.dcxs.shop.module.category.entity.JsCategoryTwo;
import com.dcxs.shop.module.category.mapper.JsCategoryThreeMapper;
import com.dcxs.shop.module.category.service.IJsCategoryOneService;
import com.dcxs.shop.module.category.service.IJsCategoryThreeService;
import com.dcxs.shop.module.category.service.IJsCategoryTwoService;
import com.dcxs.shop.module.product.entity.JsProduct;
import com.dcxs.shop.module.product.entity.JsProductPay;
import com.dcxs.shop.module.product.service.IJsProductPayService;
import com.dcxs.shop.module.product.service.IJsProductService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2020-03-01
 */
@Service
public class JsCategoryThreeServiceImpl extends ServiceImpl<JsCategoryThreeMapper, JsCategoryThree> implements IJsCategoryThreeService {


    @Autowired
    private IJsCategoryOneService categoryOneService;

    @Autowired
    private IJsCategoryTwoService categoryTwoService;

    @Autowired
    private IJsCategoryThreeService categoryThreeService;

    @Autowired
    private IJsBannerPicService bannerPicService;


    @Autowired
    private IJsProductService productService;

    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private IJsProductPayService productPayService;


    @Override
    public List<Map> categoryFirstPage() {
       //查询出所有的一级分类
        List<Map> data = new ArrayList<>();
        List<JsCategoryOne> list = categoryOneService.list();
        for(JsCategoryOne categoryOne:list){
            //查询一级分类下的二级分类
            Map  map = new HashMap();
            map.put("oneId",categoryOne.getId());
            map.put("oneName",categoryOne.getName());
            List<JsCategoryTwo> twoList = categoryTwoService.list(new QueryWrapper<JsCategoryTwo>().eq("one_id",categoryOne.getId()));
            for(JsCategoryTwo jsCategoryTwo:twoList){
                List<JsProduct> threeList = productService.list(new QueryWrapper<JsProduct>().eq("two_id",jsCategoryTwo.getId()).eq("state",1).orderByAsc("sort"));
                if(CollectionUtil.isNotEmpty(threeList)){
                    jsCategoryTwo.setProductList(threeList);
                    for(JsProduct product:threeList){
                        if(1==product.getDisState()){
                            if(!DateUtil.isIn(new Date(),Date.from(product.getDisBeginDatetime().atZone(ZoneId.systemDefault()).toInstant()),Date.from(product.getDisEndDatetime().atZone(ZoneId.systemDefault()).toInstant()))){
                                product.setDisState(0);
                            }
                        }
                        List<JsProductPay> pays = productPayService.list(new QueryWrapper<JsProductPay>().eq("product_id",product.getId()));
                        if(CollectionUtil.isNotEmpty(pays)){
                            product.setPayList(pays);
                        }
                    }
                }
            }
            map.put("twoList",twoList);
            data.add(map);
         }
        return data;
    }


    @Override
    public Map index() {
        //查询轮播图
        Map res = new HashMap();
        List<JsBannerPic> bannerPicList = bannerPicService.list();
        List<JsCategoryOne> categoryOneList = categoryOneService.list();
        List<String> list = new ArrayList<>();
        list.add("百里果园");
        list.add("百里菜园");
        list.add("金山稻米");
        list.add("特种养殖");
        List<JsProduct>  productList = productService.list(new QueryWrapper<JsProduct>().in("one_name",list).eq("promate_state",1));
        Map<String,List<JsProduct>> data = productList.stream().collect(Collectors.groupingBy(JsProduct::getOneName));
        data.put("blgyList",data.remove("百里果园"));
        data.put("blcyList",data.remove("百里菜园"));
        data.put("jsdmList",data.remove("金山稻米"));
        data.put("tsyzList",data.remove("特种养殖"));
        res.put("bannerList",bannerPicList);
        res.put("productList",data);
        res.put("categoryOneList",categoryOneList);
        return res;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String productName = (String)params.get("productName");
        String oneId = (String)params.get("oneId");
        String threeId = (String)params.get("threeId");
        IPage<JsProduct> page = productService.page(
                new Query<JsProduct>().getPage(params),
                new QueryWrapper<JsProduct>()
                        .like(StringUtils.isNotBlank(productName),"name", productName)
                        .eq(StringUtils.isNotBlank(oneId),"one_id", oneId)
                        .eq(StringUtils.isNotBlank(threeId),"three_id", threeId)
        );
        for(JsProduct product:page.getRecords()){
           String count = redisUtils.get("jinshan:product:sale:"+product.getId());
           if(StringUtils.isNotBlank(count)){
               product.setSaleCount(Integer.valueOf(count));
           }else {
               product.setSaleCount(0);
           }
            if(1==product.getDisState()){
                if(!DateUtil.isIn(new Date(),Date.from(product.getDisBeginDatetime().atZone(ZoneId.systemDefault()).toInstant()),Date.from(product.getDisEndDatetime().atZone(ZoneId.systemDefault()).toInstant()))){
                    product.setDisState(0);
                }
            }
        }
        return new PageUtils(page);
    }
}
